<template>
  <div class="medium-total-item">
    <el-table
      :data="data"
      header-cell-class-name="total-detail-header"
      fit
      style="width: 100%">
      <el-table-column label="月份" align="left">
        <template slot-scope="scope">
          {{ scope.row.time }}
        </template>
      </el-table-column>
      <el-table-column label="询单数" align="left">
        <template slot-scope="scope">{{ scope.row.orderListNum }}</template>
      </el-table-column>
      <el-table-column label="完成数量" align="left">
        <template slot-scope="scope">{{ scope.row.completeNum }}</template>
      </el-table-column>
      <el-table-column label="月度成单率" align="left">
        <template slot-scope="scope">
          <div>
            {{ scope.row.completePercent || scope.row.completePercent === 0 ? scope.row.completePercent.toFixed(1) + '%' : '--' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left">
        <template slot-scope="scope">
          <el-button class="op-btn" type="text" @click="toDetail(scope.row.time)">查看详情 <img :src="detailIcon" class="detail-icon" alt=""> </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      v-if="dialogVisible && detailList && detailList.items"
      :visible.sync="dialogVisible"
      show-close
      class="large-common-dialog-size">
      <TotalDetailList :time="time" :detail-list="detailList" :loading="loading" @currentPage="currentPage"/>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { totalOrderDetail } from '@/api/medium';
import TotalDetailList from '@/components/MediumPage/order/MediumTotalDetailList';
import detailIcon from '@/assets/medium/go_detail_icon.png';
export default {
  name: 'MediumTotalItem',
  components: { TotalDetailList },
  props: {
    data: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  data() {
    return {
      detailIcon,
      dialogVisible: false,
      detailList: [],
      page: 1,
      perPage: 20,
      loading: false,
      time: ''
    };
  },
  methods: {
    async getDetail(time, reset = false) {
      if (reset) this.page = 1;
      try {
        const params = {
          page: this.page,
          perPage: this.perPage,
          time
        };
        this.loading = true;
        const result = await totalOrderDetail(params);
        this.loading = false;
        if (result.code === 200) {
          this.detailList = result.data;
        } else {
          this.$message({ type: 'error', message: result.message });
        }
      } catch (error) {
        this.loading = false;
      }
    },
    currentPage(toPage) {
      if (toPage) {
        this.page = toPage;
        this.getDetail(this.time);
      }
    },
    toDetail(time) {
      this.dialogVisible = true;
      this.time = time;
      this.getDetail(time, true);
    }
  }
};
</script>
<style lang="scss" scoped>
.op-btn {
  display: flex;
  align-items: center;
  font-size: 14px;
}
.detail-icon {
  width: 16px;
  position: relative;
  top: 3px;
}

</style>
<style lang="scss">
.medium-total-item {
  margin-top: 20px;
  .total-detail-header {
    border-bottom: none !important;
  }
  .el-dialog {
    border-radius: 12px;
  }
  .el-dialog__body {
    padding-bottom: 0 !important;
  }
  .el-dialog__close {
    font-size: 24px;
  }
  .el-dialog__footer {
    padding-top: 25px;
    text-align: center;
    .el-button {
      width: 80px;
      height: 32px;
      background: #EFF3FB;
      border: none;
      border-radius: 4px;
      font-size: 16px;
      font-weight: 400;
      color: #406EFF;
      line-height: 16px;
    }
  }
}
</style>
